package zzzxb.generation;

import zzzxb.ui.DirectoryTOChoose;
import zzzxb.ui.FileInfo;
import zzzxb.utils.Util;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author Zzzxb  2019/8/10 8:57
 * @description: sine qua non rule
 */
public class ConditionalRules {
    private RWOperation rwOperation;
    private Map<String, String> sysInfoMap;
    public static Format format;
    DirectoryTOChoose directoryTOChoose;
    FileInfo fileInfo;

    public ConditionalRules() {
        rwOperation = new RWOperation();
        sysInfoMap = System.getenv();
        format = new Format(sysInfoMap.get("USERNAME"));
    }

    public void init() {
        if (!new File(format.getBlogModesPath()+"\\rootPath").exists()) {
            directoryTOChoose = new DirectoryTOChoose();
            while (format.getFilePath() == null) {
                System.out.println("hi");
            }
            openOnce();
            System.out.println("初始化配置信息");
            format.getModeList();
            directoryTOChoose.dispose();
        }
        System.out.println("路径配置成功!");
        System.out.println(format.getFileName());
        fileInfo = new FileInfo();
        while (format.getFileName() == null && format.getTag() == null) {
            format.getModeList();
        }
        fileInfo.dispose();
        format.setMode(0);
        System.out.println("文件信息获取完毕");
        createFile();
        System.out.println("文件创建成功");
    }

    // 首次打开
    public void openOnce() {
        String path = format.getBlogModesPath();
        File file = new File(path + "\\mods");
        File file1 = new File(path + "\\rootPath");
        File file2 = new File(path + "\\mods\\null.md");
        File file3 = new File(path + "\\mods\\jekyllBlog.md");

        // 配置目录不存在，则创建目录
        if (!file.exists()) {
            file.mkdirs();
        }
        // 初始化mod
        if (!file2.exists() || !file3.exists()) {
            try {
                rwOperation.writeFile(path + "\\mods\\null.md", "@{title}");
                rwOperation.writeFile(path + "\\mods\\jekyllBlog.md",
                        Util.getJekyllModeInfo());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        // rootPath 不存在，则创建
        if (!file1.exists()) {
            try {
                rwOperation.writeFile(path + "\\rootPath", format.getFilePath());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 根据 mode 创建文件
    public void createFile() {
        String path = format.getBlogModesPath();
        try {
            format.setFilePath(rwOperation.readFile(path+"\\rootPath"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 获取根目录地址
        try {
            // 获取指定mod内容
            String modContent =
                    rwOperation.readFile(format.getBlogModesPath()+"\\mods\\" +
                            format.getMode());
            File fileTag = null;
            if (!(fileTag = new File(format.getFilePath()+
                    "\\"+format.getTag())).exists())
                fileTag.mkdir();

            rwOperation.writeFile(format.getFilePath()+"\\" + format.getTag() + "\\" +
                            LocalDate.now().toString()+ "-" +format.getFileName()+ ".md",
                    formatModContent(modContent));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String formatModContent(String modContent) {
        String content = modContent;
        content = content.replace("@{title}", format.getTitle());
        content = content.replace("@{date}", LocalDate.now().toString());
        content = content.replace("@{time}",
                LocalDateTime.now().getHour()+":"+LocalDateTime.now().getMinute()+
                        ":"+ LocalDateTime.now().getSecond());
        content = content.replace("@{tag}", format.getTag());
        return content;
    }
}
